package com.ruoyi.app.zzq.service.impl;


import com.ruoyi.app.dyh.hxf.domain.Message;
import com.ruoyi.app.dyh.hxf.service.IMessageService;
import com.ruoyi.app.zzq.domain.Team;
import com.ruoyi.app.zzq.domain.User;
import com.ruoyi.app.zzq.mapper.MigrateMapper;
import com.ruoyi.app.zzq.service.MigrateService;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MigrateServiceImpl implements MigrateService {
    @Autowired
    MigrateMapper migrateMapper;
    @Autowired
    IMessageService messageService;
    @Override
    public Team SelectServerByid(String id) {
        Team team=migrateMapper.SelectServerByid(id);
        //通过id获取领导人信息
        User user=migrateMapper.SelectUserById(team.getLeaderId());
        team.setLeaderName(user.getUserName());
        //获取组织人数
        team.setNum(""+migrateMapper.SelectServermember(id).size());
        return team;
    }

    @Override
    public List<User> SelectServermember(String teamid) {
        return migrateMapper.SelectServermember(teamid);
    }

    @Override
    public int DisbandServerTeam(String teamid) {
        return migrateMapper.DisbandServerTeam(teamid);
    }

    @Override
    public int ServerCaptainTransfer(String teamid, String userid, String captain) {
        return migrateMapper.ServerCaptainTransfer( teamid,  userid,  captain);
    }

    @Override
    public List<Team> SelectMyServerByid(String id) {
        //通过id获得
        return migrateMapper.SelectMyServerByid(id);
    }

    @Override
    public boolean Isjoin(String teamid, String userid) {
        User user=migrateMapper.Isjoin(teamid,userid);
        if(user==null)
        {
            return true;
        }else {
            return false;
        }
    }

    @Override
    public int joinServer(String teamid, String s) {
        Message message=new Message();
        String username=migrateMapper.getNameByid(s);
        message.setCreateBy(username);
        message.setCreateTime(DateUtils.getNowDate());
        message.setIsDelete("1");
        String leaderid=migrateMapper.isTeamLeader(teamid);
        message.setReceiverId(String.valueOf(Integer.valueOf(leaderid)));
        message.setMgsStatus(3);
        message.setMsgTitle("加入了服务队");
        message.setMsgContent("加入了服务队");
        messageService.insertMessage(message);
        //获取原组织名称
        User user=migrateMapper.SelectUserById(s);
        if(!user.getGroupId().equals("")) {
            teamid = user.getTeamId() + "," + teamid;
        }
        return migrateMapper.joinServer(teamid,s);
    }

    @Override
    public int outServer(String teamid, String s) {
        Message message=new Message();
        String username=migrateMapper.getNameByid(s);
        message.setCreateBy(username);
        message.setCreateTime(DateUtils.getNowDate());
        message.setIsDelete("1");
        String leaderid=migrateMapper.isTeamLeader(teamid);
        message.setReceiverId(String.valueOf(Integer.valueOf(leaderid)));
        message.setMgsStatus(3);
        message.setMsgTitle("退出了服务队");
        message.setMsgContent("退出了服务队");
        messageService.insertMessage(message);
        //获取原组织名称
        User user=migrateMapper.SelectUserById(s);
        String id1=user.getTeamId().replace(","+teamid,"");
        String id2=id1.replace(teamid+",","");
        return migrateMapper.joinServer(id2,s);
    }

    @Override
    public String IsjoinHeart(String s) {
        //用户信息
        User user=migrateMapper.SelectUserById(s);
        if(user.getGroupId().equals(""))
        {
            return ""+-1;
        }else {
            return user.getGroupId();
        }
    }

    @Override
    public String joinHeart(String s) {
        //用户信息
        User user=migrateMapper.SelectUserById(s);
        //获取所有暖心团领导人id
        List<String> list=migrateMapper.selectWarmLeaderIds();
        String Leaderid="";

        //查询领导人党支部是否和用户一致
        for (String s1 : list) {
            String pdept_id=migrateMapper.selectPdeptidByid(s1);
            if(user.getPdeptId().equals(pdept_id))
            {
                Leaderid=s1;
                break;
            }
        }
        //查询领导人所在暖心团
        String teamid=migrateMapper.selectTeamidByid(Leaderid);
        Message message=new Message();
        String username=migrateMapper.getNameByid(s);
        message.setCreateBy(username);
        message.setCreateTime(DateUtils.getNowDate());
        message.setIsDelete("1");
        message.setReceiverId(String.valueOf(Integer.valueOf(Leaderid)));
        message.setMgsStatus(3);
        message.setMsgTitle("加入了暖心团");
        message.setMsgContent("加入了暖心团");
        messageService.insertMessage(message);
        //加入暖心团
        migrateMapper.joinHeart(s,teamid);
        return teamid;
    }

    @Override
    public List<Team> SelectServerBykey(String key) {
        return migrateMapper.SelectServerBykey(key);
    }

    @Override
    public boolean isTeamLeader(String teamid, String s) {
        String Leaderid= migrateMapper.isTeamLeader(teamid);
        if(Leaderid.equals(s))
        {
            return true;
        }else {
            return false;
        }
    }

    @Override
    public int outHeart(String s) {
        User user=migrateMapper.SelectUserById(s);
        //获取所有暖心团领导人id
        List<String> list=migrateMapper.selectWarmLeaderIds();
        String Leaderid="";

        //查询领导人党支部是否和用户一致
        for (String s1 : list) {
            if(user.getPdeptId().equals(migrateMapper.selectPdeptidByid(s1)));
            {
                Leaderid=s1;
                break;
            }
        }
        Message message=new Message();
        String username=migrateMapper.getNameByid(s);
        message.setCreateBy(username);
        message.setCreateTime(DateUtils.getNowDate());
        message.setIsDelete("1");
        message.setReceiverId(String.valueOf(Integer.valueOf(Leaderid)));
        message.setMgsStatus(3);
        message.setMsgTitle("退出了暖心团");
        message.setMsgContent("退出了暖心团");
        messageService.insertMessage(message);
        return migrateMapper.outHeart(s);
    }

    @Override
    public int KillTeam(String teamid) {
        return migrateMapper.KillTeam(teamid);
    }

    @Override
    public List<User> getHeartMember(String teamid) {
        return migrateMapper.getHeartMember(teamid);
    }

}
